package com.oa.ylms.apps.actions.limit;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.limit.ILimitInfoService;
import com.oa.ylms.apps.business.limit.IPostLimitService;
import com.oa.ylms.apps.business.organization.IPostInfoService;
import com.oa.ylms.apps.business.organization.ISectionInfoService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.entity.limit.LimitInfoValue;
import com.oa.ylms.apps.entity.organization.PostInfoValue;
import com.oa.ylms.apps.entity.organization.SectionInfoValue;

@SuppressWarnings("serial")
public class PostLimitAction extends BasicAction {
	private List<LimitInfoValue> allData ;
	private ILimitInfoService limitService;
	private IPostLimitService postLimitService;
	private ISectionInfoService sectionService;
	private IPostInfoService postService;
	private List<SectionInfoValue> sectionList;
	private Map<String, Object> map;
	
	private String str;
	private StringBuilder writer = new StringBuilder();
	String liElement = "<input name=\"geo\" id=\"chk_{0}\" value=\"{0}\" type=\"checkbox\" /><label>{1}</label>";
	String liParent = "<input value=\"{0}\"  id=\"chk_{0}\" type=\"checkbox\" class=\"parent\" parent=\"true\" /><label>{1}</label>";
	
	private Integer postId;
	private Integer sectionId;
	private String limitIds;
     
	public String execute() throws Exception{
		allData=limitService.queryLimitInfoWithPage(1000000, 1);
		sectionList = sectionService.querySectionInfo(null, IDEDStates.ENABLE);
		str=buildTree();
		return SUCCESS;
	}
	
	public void makeTree(List<LimitInfoValue> childrenList)
	{
		if(childrenList.size()>0)
		{
			writer.append("<ul>");
			for(LimitInfoValue liv:childrenList)
			{
				writer.append("<li>");
				List<LimitInfoValue> nexts = getChildrenByParent(liv.getLimitId());
				boolean hasChildren = nexts.size()>0;
				/*writer.append(String.format(hasChildren?liParent:liElement,
					liv.getLimitId(),liv.getLimitName()));*/
				if(hasChildren){
					writer.append("<input value=\""+liv.getLimitId()+"\"  id=\"chk_"+liv.getLimitId()+"\" class=\"parentNode\" parent=\"true\" type=\"checkbox\" /><label>"+liv.getLimitName()+"</label>");
				}else{
					writer.append("<input name=\"geo\" id=\"chk_"+liv.getLimitId()+"\" value=\""+liv.getLimitId()+"\" type=\"checkbox\" /><label>"+liv.getLimitName()+"</label>");
				}
				makeTree(nexts);
				writer.append("</li>");				
			}
			writer.append("</ul>");
		}
	}
	
	public String buildTree()
    {
        List<LimitInfoValue> list = getChildrenByParent(0);

        writer.append("<div  class=\"treeBody\"><ul id=\"limitInfoTree\" class=\"tree\">");

        for (LimitInfoValue item:list)
        {
			writer.append("<li>");
			List<LimitInfoValue> children = getChildrenByParent(item.getLimitId());
			boolean hasChildren = children.size() > 0;
			if(hasChildren){
				writer.append("<input value=\""+item.getLimitId()+"\"  id=\"chk_"+item.getLimitId()+"\" class=\"parentNode\" parent=\"true\" type=\"checkbox\" /><label>"+item.getLimitName()+"</label>");
			}else{
				writer.append("<input name=\"geo\" id=\"chk_"+item.getLimitId()+"\" value=\""+item.getLimitId()+"\" type=\"checkbox\" /><label>"+item.getLimitName()+"</label>");
			}
			makeTree(children);
			writer.append("</li>");
		}
		writer.append("</ul></div>");
		return (writer.toString());
    }
	
	public List<LimitInfoValue> getChildrenByParent(int parentLimitId)
	{
		List<LimitInfoValue> result=new ArrayList<LimitInfoValue>();
		
		for(LimitInfoValue liv:allData)
		{
			if(liv.getParentId()==parentLimitId)
				result.add(liv);
		}
		return result;
	}
	
	public void mainActionCalledFromAjax() throws Exception
	{ 
	       String result = getLeafLimitIdsArrayStringByPostId(postId);
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			 
			response.getWriter().println(result); 
	 }


	public String getLeafLimitIdsArrayStringByPostId(int postId) throws Exception
	{
	      String limitIdsFromOnePostId = postLimitService.queryByPostId(postId).getLimitIds();
  	 
	      List<LimitInfoValue> list = limitService.queryByLimitIds(limitIdsFromOnePostId);
 
	      StringBuilder result = new StringBuilder("[");
	      for(LimitInfoValue limit  : list)
	      { 
	                result.append(limit.getLimitId()+",");
	      }
	      int len =result.length();
 
	      return result.substring(0,len-1)+"]";
	}
	
	public String addOrModify() throws Exception{
		if(postLimitService.addOrModifyPostLimit(postId, limitIds)){
			response.getWriter().write("success");
		}
		return null;
	}

	public String queryPostBySection() throws Exception{
		 
		if(sectionId != null){
			Gson gson = new Gson();
			List<PostInfoValue>  postList = postService.queryPostInfoBySectionId(sectionId);
			String str = gson.toJson(postList);
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().println(str);
		}
		return null;
	}

	public List<LimitInfoValue> getAllData() {
		return allData;
	}

	public void setAllData(List<LimitInfoValue> allData) {
		this.allData = allData;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public StringBuilder getWriter() {
		return writer;
	}

	public void setWriter(StringBuilder writer) {
		this.writer = writer;
	}

	public ILimitInfoService getLimitService() {
		return limitService;
	}

	public void setLimitService(ILimitInfoService limitService) {
		this.limitService = limitService;
	}

	public IPostLimitService getPostLimitService() {
		return postLimitService;
	}

	public void setPostLimitService(IPostLimitService postLimitService) {
		this.postLimitService = postLimitService;
	}

	public Integer getPostId() {
		return postId;
	}

	public void setPostId(Integer postId) {
		this.postId = postId;
	}

	public void setSectionService(ISectionInfoService sectionService) {
		this.sectionService = sectionService;
	}

	public ISectionInfoService getSectionService() {
		return sectionService;
	}

	public void setPostService(IPostInfoService postService) {
		this.postService = postService;
	}

	public IPostInfoService getPostService() {
		return postService;
	}

	public void setSectionList(List<SectionInfoValue> sectionList) {
		this.sectionList = sectionList;
	}

	public List<SectionInfoValue> getSectionList() {
		return sectionList;
	}

	public void setSectionId(Integer sectionId) {
		this.sectionId = sectionId;
	}

	public Integer getSectionId() {
		return sectionId;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setLimitIds(String limitIds) {
		this.limitIds = limitIds;
	}

	public String getLimitIds() {
		return limitIds;
	}
}